//
//  FeedbackViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-8-22.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "FeedbackViewController.h"
#import "ServerInterface.h"
#import "Contact.h"

extern Contact *globalContact;
@interface FeedbackViewController (){

}
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *wordNumLabel;

@end

@implementation FeedbackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(postFeedback:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    _textView.text = @"反馈建议";
    self.title = @"抁建议";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)postFeedback:(id)sender{
    if (_textView.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"内容不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    [self.navigationController popViewControllerAnimated:YES];
    NSDictionary *   feedbackDic = @{@"content":_textView.text,@"user_id":@(globalContact.userID), @"company_id":CompanyID,@"token":globalContact.token};
    [ServerInterface feedbackWithDic:feedbackDic withBlock:^(NSDictionary *resultDic, NSError *error) {
        if (error) {
        }
        else{
            if ([[resultDic valueForKeyPath:@"success"] integerValue] ==1) {
                BOOL success =[Contact  updateContact:globalContact];
                if (success) {
                }
            }
            else{
                NSString *message =[resultDic objectForKey:@"message"];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
    }];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    
}


- (void)textViewDidChange:(UITextView *)textView{
    _wordNumLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)textView.text.length];
}

@end
